package cn.jtool.dig.common.metrics;

import com.google.common.collect.Maps;

import java.util.Map;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicLong;

public class MetricManager {

    private static Map<MetricMeta, AtomicLong> metricMetaMap = Maps.newConcurrentMap();

    public static synchronized long updateMetric(MetricMeta meta, long value){
        AtomicLong metricValue = metricMetaMap.get(meta);
        if(Objects.isNull(metricValue)){
            metricValue = new AtomicLong(0);
        }
        long number = metricValue.getAndAdd(value);
        metricMetaMap.put(meta, metricValue);
        return number;
    }

    public static long getMetric(MetricMeta meta){
        AtomicLong value = metricMetaMap.get(meta);
        return Objects.isNull(value) ? 0 : value.get();
    }

}
